package ru.rchervot.sanasar.client.place;

import ru.rchervot.sanasar.client.model.FactoryQueryCandidates;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.view.FactoryModuleSettings;
import ru.rchervot.sanasar.client.view.RootPresenter;

import com.google.gwt.event.shared.HandlerManager;
import com.google.inject.Inject;

public class FactoryPlaceSettings implements FactoryPlace {
	
	private final HandlerManager eventBus;
	private final RPCServiceAsync service;
	private final FactoryModuleSettings factoryModule;
	private final RootPresenter rootPresenter;
	
	@Inject
	public FactoryPlaceSettings(HandlerManager eventBus,
			RPCServiceAsync service, FactoryQueryCandidates factoryQuery,
			RootPresenter rootPresenter, FactoryModuleSettings factoryModule) {
		this.eventBus = eventBus;
		this.service = service;
		this.rootPresenter = rootPresenter;
		this.factoryModule = factoryModule;
	}
	
	@Override
	public Place buildPlace(HistoryTokenObject hto) {
		return getPlaceDefault();
	}
	
	private Place placeDefault;
	private Place getPlaceDefault() {
		if (placeDefault == null) {
			placeDefault = new PlaceSettings(
					eventBus, service, rootPresenter, factoryModule);
		}
		
		currentPlace = placeDefault;
		
		return currentPlace;
	}

	private Place currentPlace;
	@Override
	public Place getCurrentPlace() {
		if (currentPlace != null) {
			return currentPlace;			
		} else {
			return getPlaceDefault();
		}
	}
	
}